package com.ktwlsoft.archivemanage.domain.business.businessService;

import com.github.pagehelper.PageInfo;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.QuestionRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.Account;
import com.ktwlsoft.archivemanage.domain.dao.entity.Question;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.question.QuestionGetByIdResponse;
import com.ktwlsoft.archivemanage.payload.question.QuestionPageRequest;
import com.ktwlsoft.archivemanage.payload.question.QuestionSaveRequest;
import com.ktwlsoft.archivemanage.payload.question.QuestionUpdateRequest;
import com.ktwlsoft.archivemanage.service.security.UserPrincipal;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.util.Date;

/**
 * @author JokerMo
 * @version 1.0
 * @date 2019/12/12 14:18
 */
@Component
public class QuestionService {

    /**
     * 问题
     */
    private final QuestionRepositoryService questionRepositoryService;


    public QuestionService(QuestionRepositoryService questionRepositoryService) {
        this.questionRepositoryService = questionRepositoryService;
    }

    /**
     * 问题列表
     *
     * @return Result<?>
     */
    public Result<?> findPage(QuestionPageRequest request) {
        PageInfo<Question> pageInfo = questionRepositoryService.findPage(request);
        return Result.ok(pageInfo);
    }

    /**
     * 问题详情
     *
     * @param id id
     * @return Result<?>
     */
    public Result<?> getById(Long id) throws UnsupportedEncodingException {
        Question question = questionRepositoryService.getById(id);
        if (question == null) {
            return Result.body(id, ResultType.CHECK_ERROR).withMessage("该数据不存在");
        }
        QuestionGetByIdResponse questionGetByIdResponse = new QuestionGetByIdResponse();
        BeanUtils.copyProperties(question, questionGetByIdResponse);
        String content = new String(question.getContent(), "utf-8");
        questionGetByIdResponse.setContent(content);
        return Result.ok(questionGetByIdResponse);
    }

    /**
     * 问题新增
     *
     * @param request request
     * @return Result<?>
     */
    public Result<?> save(QuestionSaveRequest request) throws UnsupportedEncodingException {
        Question question = new Question();
        BeanUtils.copyProperties(request, question);
        byte[] content = request.getContent().getBytes("utf-8");
        question.setContent(content);
        question.setState(request.getUp() ? Question.State.UP : Question.State.WAIT_UP);
        try {
            questionRepositoryService.insert(question);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.body(null, ResultType.SAVING_ERROR);
        }
        return Result.ok(question.getId());
    }

    /**
     * 问题修改
     *
     * @param request request
     * @return Result<?>
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> edit(QuestionUpdateRequest request) {
        Question question = questionRepositoryService.getById(request.getId());
        if (question == null) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("该数据不存在");
        }
        Account account = UserPrincipal.getCurrentUser();
        Question updateQuestion = new Question();
        BeanUtils.copyProperties(request, updateQuestion);
        updateQuestion.setContent(request.getContent().getBytes());
        updateQuestion.setState(request.getUp() ? Question.State.UP : question.getState());
        updateQuestion.setOperater(account != null ? account.getRealName() : "");
        updateQuestion.setOperaterId(account != null ? account.getId() : null);
        try {
            questionRepositoryService.update(updateQuestion);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.body(null, ResultType.UPDATE_ERROR);
        }
        return Result.ok(question.getId());
    }

    /**
     * 问题删除
     *
     * @param id id
     * @return Result<?>
     */
    public Result<?> deleteById(Long id) {
        Question question = questionRepositoryService.getById(id);
        if (question == null) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("数据不存在");
        }
        try {
            questionRepositoryService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.body(null, ResultType.DELETE_ERROR);
        }
        return Result.ok(question.getId());
    }

    /**
     * 问题上下架
     *
     * @param id
     * @param state
     * @return
     */
    public int upOrDownById(Long id, Question.State state) {
        Account account = UserPrincipal.getCurrentUser();
        String realName = account != null ? account.getRealName() : null;
        Long accountId = account != null ? account.getId() : null;
        return questionRepositoryService.getQuestionMapper().upOrDownById(id, state, new Date(), realName, accountId);
    }
}
